"use strict";

var crypto = require('crypto'),
    C = require('../common'),
    API = function () {
    this.isFake = true;

    this.getUser = function (openid, callback) {
        if (openid === 'o6_bmjrPTlm6_2sgVt7hMZOPfL2M') {
            callback(null, {
                "subscribe": 1,
                "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
                "nickname": "Band",
                "sex": 1,
                "language": "zh_CN",
                "city": "广州",
                "province": "广东",
                "country": "中国",
                "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
                "subscribe_time": 1382694957
            });
        } else if (openid === 'o7_bmjrPTlm6_2sgVt7hMZOPfL2M') {
            callback(null, {
                "subscribe": 0,
                "openid": "o7_bmjrPTlm6_2sgVt7hMZOPfL2M"
            });
        } else {
            callback({"errcode":40013, "errmsg":"invalid appid"});
        }
    };
};

var OAuth = function () {
    this.isFake = true;

    this.getUser = function (openid, callback) {
        if (openid === 'o6_bmjrPTlm6_2sgVt7hMZOPfL2M') {
            callback(null, {
                "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
                "nickname": "Band2",
                "sex": 1,
                "city": "广州",
                "province": "广东",
                "country": "中国",
                "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
                "privilege":[
                    "PRIVILEGE1",
                    "PRIVILEGE2"
                ]
            });
        } else if (openid === 'o8_bmjrPTlm6_2sgVt7hMZOPfL2M') {
            callback(null, {
                "openid": "o8_bmjrPTlm6_2sgVt7hMZOPfL2M",
                "nickname": "NICKNAME",
                "sex": "1",
                "province": "PROVINCE",
                "city": "CITY",
                "country": "COUNTRY",
                "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
                "privilege":[
                    "PRIVILEGE1",
                    "PRIVILEGE2"
                ]
            });
        } else {
            callback({"errcode": 40003, "errmsg": "invalid openid"});
        }
    };

    this.getAccessToken = function (code, callback) {
        if (code === 'CODE') {
            callback(null, {
                "access_token": "ACCESS_TOKEN",
                "expires_in": 7200,
                "refresh_token": "REFRESH_TOKEN",
                "openid": "o8_bmjrPTlm6_2sgVt7hMZOPfL2M",
                "scope": "SCOPE"
            });
        }
    };
};

var _api = new API(), _oauth = new OAuth(),
    WechatApiService = function () {
        return _api;
    },
    WechatOAuthService = function () {
        return _oauth;
    };

WechatOAuthService.sign = function (url, secret) {
    var shasum = crypto.createHash('md5');
    shasum.update(url, 'utf8');
    shasum.update(secret, 'utf8');

    return shasum.digest('base64').substr(0, 8);
};

module.exports = {

    type: 'service',

    load: function (app, config, cb) {
        if (ENV === 'production') {
            throw new C.Error.ConfigError('global[' + ENV + '].features', '"fakewechat" is not designed for production use!');
        }

        app.registerService('wechatApi', WechatApiService);
        app.registerService('wechatOAuth', WechatOAuthService);

        cb();
    }
};


